#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

priority_queue<int, vector<int>, greater<int>> q;

int n;
int a[10010];
int energy = 0;

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    if (n == 1)
    {
        cout << a[1] << endl;
        return 0;
    }
    else if (n == 2)
    {
        cout << a[1] + a[2] << endl;
    }
    else
    {
        for (int i = 1; i <= n; i++)
            q.push(a[i]);
        while (q.size() != 1)
        {
            int temp1 = q.top();
            q.pop();
            int temp2 = q.top();
            q.pop();
            energy += (temp1 + temp2);
            q.push(temp1 + temp2);
        }
        cout << energy << endl;
    }
    //system("pause");
    return 0;
}